import java.util.*;

class Solution1 {
    public boolean canFinish(int n, int[][] p) {
        //1.准备工作
        int[] in = new int[n];   //统计每一个顶点的入度
        Map<Integer, List<Integer>> edges = new HashMap<>(); //邻接表图

        //2.建图
        for (int i = 0; i < p.length; i++) {
            int a = p[i][0], b = p[i][1]; //b -> a
            if (!edges.containsKey(b)) {
                edges.put(b, new ArrayList<>());
            }
            edges.get(b).add(a);
            in[a]++;
        }

        //3.拓扑排序
        Queue<Integer> q = new LinkedList<>();
        //(1) 先把入读为0的点加入到队列中
        for (int i = 0; i < n; i++) {
            if (in[i] == 0) {
                q.add(i);
            }
        }

        //(2) bfs
        while (!q.isEmpty()) {
            int t = q.poll();
            for (int a : edges.getOrDefault(t, new ArrayList<>())) {
                in[a]--;
                if (in[a] == 0) q.add(a);
            }
        }

        for (int i = 0; i < n; i++) {
            if (in[i] != 0) {
                return false;
            }
        }
        return true;
    }
}

class Solution2 {
    public boolean canFinish(int n, int[][] p) {
        HashMap<Integer, List<Integer>> map = new HashMap<>();
        int[] in = new int[n];
        //建图
        for (int i = 0; i < p.length; i++) {
            int a = p[i][0], b = p[i][1]; // b -> a
            if (!map.containsKey(b)) {
                map.put(b, new ArrayList<>());
            }
            map.get(b).add(a);
            in[a]++;
        }

        Queue<Integer> queue = new LinkedList<>();
        for (int i = 0; i < n; i++) {
            if (in[i] == 0) {
                queue.add(i);
            }
        }

        while (!queue.isEmpty()) {
            int t = queue.poll();
            for (int i : map.getOrDefault(t, new ArrayList<>())) {
                in[i]--;
                if (in[i] == 0) queue.add(i);
            }
        }

        for (int i = 0; i < n; i++) {
            if (in[i] != 0) return false;
        }
        return true;
    }
}

class Solution3 {
    public int[] findOrder(int n, int[][] p) {
        HashMap<Integer, List<Integer>> map = new HashMap<>();
        int[] in = new int[n];
        //建图
        for (int i = 0; i < p.length; i++) {
            int a = p[i][0], b = p[i][1]; // b -> a
            if (!map.containsKey(b)) {
                map.put(b, new ArrayList<>());
            }
            map.get(b).add(a);
            in[a]++;
        }

        Queue<Integer> queue = new LinkedList<>();
        for (int i = 0; i < n; i++) {
            if (in[i] == 0) {
                queue.add(i);
            }
        }

        int[] ret = new int[n];
        int j = 0;
        while (!queue.isEmpty()) {
            int t = queue.poll();
            ret[j++] = t;
            for (int i : map.getOrDefault(t, new ArrayList<>())) {
                in[i]--;
                if (in[i] == 0) queue.add(i);
            }
        }

        if (j == 0) {
            return new int[0];
        }
        return ret;
    }
}

class Solution4 {
    Map<Character, Set<Character>> edges = new HashMap<>(); //邻接表
    Map<Character, Integer> in = new HashMap<>(); //入度信息
    boolean check;

    public String alienOrder(String[] words) {
        //1.初始化入度哈希表
        for (String s : words) {
            for (int i = 0; i < s.length(); i++) {
                char ch = s.charAt(i);
                in.put(ch, 0);
            }
        }
        int n = words.length;
        for (int i = 0; i < n; i++) {
            for (int j = i + 1; j < n; j++) {
                add(words[i], words[j]);
                if (check == true) return "";
            }
        }

        //拓扑排序
        Queue<Character> q = new LinkedList<>();
        for (char ch : in.keySet()) {
            if (in.get(ch) == 0) q.add(ch);
        }
        StringBuffer ret = new StringBuffer();
        while (!q.isEmpty()) {
            char t = q.poll();
            ret.append(t);

            if (!edges.containsKey(t)) continue;

            for (char ch : edges.get(t)) {
                in.put(ch, in.get(ch) - 1);
                if (in.get(ch) == 0) q.add(ch);
            }
        }

        for (char ch : in.keySet()) {
            if (in.get(ch) != 0) return "";
        }
        return ret.toString();
    }

    public void add(String s1, String s2) {
        int n = Math.min(s1.length(), s2.length());
        int i = 0;
        for (; i < n; i++) {
            char c1 = s1.charAt(i), c2 = s2.charAt(i);
            if (c1 != c2) {
                //c1 -> c2
                if (!edges.containsKey(c1)) {
                    edges.put(c1, new HashSet<>());
                }
                if (!edges.get(c1).contains(c2)) {
                    edges.get(c1).add(c2);
                    in.put(c2, in.get(c2) + 1);
                }
                break;
            }
        }
        if (i == s2.length() && i < s1.length()) check = true;
    }
}

class Solution {
    Map<Character,Set<Character>> edges = new HashMap<>();
    Map<Character, Integer> in = new HashMap<>();
    boolean check;
    public String alienOrder(String[] words) {
        int n = words.length;
        for(String word : words) {
            for(int i = 0; i<word.length(); i++) {
                char ch = word.charAt(i);
                in.put(ch,0);
            }
        }
        for(int i = 0; i<n; i++) {
            for(int j = i+1; j<n; j++) {
                add(words[i],words[j]);
                if(check == true) return "";
            }
        }

        Queue<Character> q = new LinkedList<>();
        for(char ch : in.keySet()) {
            if(in.get(ch) == 0) {
                q.add(ch);
            }
        }

        StringBuffer ret = new StringBuffer();
        while(!q.isEmpty()) {
            char t = q.poll();
            ret.append(t);

            if(!edges.containsKey(t)) continue;

            for(char ch:edges.get(t)) {
                in.put(ch,in.get(ch)-1);
                if(in.get(ch) == 0) q.add(ch);
            }
        }

        for(char ch : in.keySet()) {
            if(in.get(ch) != 0) return "";
        }
        return ret.toString();
    }

    public void add(String s1, String s2) {
        int n = Math.min(s1.length(),s2.length());
        int i = 0;
        for(; i<n; i++) {
            char c1 = s1.charAt(i), c2 = s2.charAt(i);
            if(c1!=c2) {
                if(!edges.containsKey(c1)) {
                    edges.put(c1, new HashSet<>());
                }
                if(!edges.get(c1).contains(c2)) {
                    edges.get(c1).add(c2);
                    in.put(c2, in.get(c2) + 1);
                }
                break;
            }
        }
        if(i == s2.length() && i<s1.length()) {
            check = true;
        }
    }
}

public class Test {

}
